//===================================================获取用户ID=========================================================
var id=$.cookie("MYID");
//var id="7c7fdd2ef9a51aed";
if(id){

}else{
    location.href="index.html";
}
var roomidreg=/(roomid=)([a-zA-Z0-9]*)/;
var roomid = urlfrom.match(roomidreg).splice("=")[2];
console.log("roomid:"+roomid);
//聊天页面返回键
$("#chatpagegobackbtn").click(function(){
    history.go(-1);
});
//环信建立连接
var conn = new WebIM.connection({
    https: WebIM.config.https,
    url: WebIM.config.xmppURL,
    isAutoLogin: WebIM.config.isAutoLogin,
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions
});
//获取聊天对象信息
var otherimg="";//保存他人头像
var myimg="";//保存自己头像
//自己的信息
$.ajax({
    type: "post",
    //提交的网址
    url: userurl,
    //提交的数据
    data: { "userId": id},
    //返回数据的格式
    datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
    success:function(data){
        if(data.Data.userAva){
            myimg="<img src='http://42.62.66.198:8003/"+data.Data.userAva+"'>";
        }else{
            myimg="<img src='img/loseimg.png'>";
        }
    }
});
//时间方法---------------------------------------------------------
function GetDateT(){
    var d,s;
    d = new Date();
    //s = d.getYear() + "-";             //取年份
    //s = s + (d.getMonth() + 1) + "-";//取月份
    s += d.getDate() + " ";         //取日期
    s += d.getHours() + ":";       //取小时
    s += d.getMinutes() + ":";    //取分
    //s += d.getSeconds();         //取秒
    return(s);
}
var oldtime=GetDateT();//当前时间
//保存对应图片-----------------------------------------------------
var map={
    '[):]':'ee_1.png',
    '[:D]': 'ee_2.png',
    '[;)]': 'ee_3.png',
    '[:-o]': 'ee_4.png',
    '[:p]': 'ee_5.png',
    '[(H)]': 'ee_6.png',
    '[:@]': 'ee_7.png',
    '[:s]': 'ee_8.png',
    '[:$]': 'ee_9.png',
    '[:(]': 'ee_10.png',
    '[:\'(]': 'ee_11.png',
    '[:|]': 'ee_12.png',
    '[(a)]': 'ee_13.png',
    '[8o|]': 'ee_14.png',
    '[8-|]': 'ee_15.png',
    '[+o(]': 'ee_16.png',
    '[<o)]': 'ee_17.png',
    '[|-)]': 'ee_18.png',
    '[*-)]': 'ee_19.png',
    '[:-#]': 'ee_20.png',
    '[:-*]': 'ee_21.png',
    '[^o)]': 'ee_22.png',
    '[8-)]': 'ee_23.png',
    '[(|)]': 'ee_24.png',
    '[(u)]': 'ee_25.png',
    '[(S)]': 'ee_26.png',
    '[(*)]': 'ee_27.png',
    '[(#)]': 'ee_28.png',
    '[(R)]': 'ee_29.png',
    '[({)]': 'ee_30.png',
    '[(})]': 'ee_31.png',
    '[(k)]': 'ee_32.png',
    '[(F)]': 'ee_33.png',
    '[(W)]': 'ee_34.png',
    '[(D)]': 'ee_35.png'
};
//登录
var options = {
    apiUrl: WebIM.config.apiURL,
    user:id,
    pwd:id,
    appKey: "pulu#pulu"
};
conn.open(options);
//环信保存用户信息 及 成员信息---------------------------------------------------------------
//var masterid="";//群主ID
var memberids=[];
//收信息--获取成员头像----------------------------------------------------------------
//获取环信成员信息----------------------------------
var queryRoomMember = function () {
    var member = '';
    conn.queryRoomMember({
        roomId: roomid,
        success: function (members) {
            if(members&&members.length>0){
                var tim= 0,len=members.length;
                for (var o in members) {
                    tim++;
                    member = members[o].jid;
                    //console.log("群组成员：",member);
                    //console.log(member.match(/^[^pulu\#pulu_][a-zA-Z0-9]/)[0]);
                    memberids.push(member.match(/[pulu#pulu_0-9a-zA-Z]+/)[0].replace("pulu#pulu_",""));
                    if(tim==len){
                        console.log("保存的群组成员id:",memberids);
                        //获取服务器所有成员信息----------------------------
                        $.ajax({
                            type:"post",
                            dataType: "json",
                            url:gethxmemberurl,
                            data:{"persons":memberids},
                            success:function(m){
                                console.log("获取的成员头像");
                                console.log(m);
                                if(m.Data&&m.Data.length>0){
                                    for(var i=0;i<m.Data.length;i++){
                                        $("<div class='imgbox'> <div> <img src='"+imgurl+m.Data[i].headImg+"'/> </div> <div class='username'>"+m.Data[i].nick+"</div> </div>").insertBefore("#addmemberbtn");
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }
    });
};
conn.listen({
    onOpened: function ( message ) {          //连接成功回调
        //如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
        conn.setPresence();
        console.log("连接成功:");
        //console.log(message);
        // 获取群组信息------------------------------------------------------------------
        var roomidreg=/(roomid=)([a-zA-Z0-9]*)/;
        var roomid = urlfrom.match(roomidreg).splice("=")[2];
        //获取群信息--------------------------------------
        var queryGroupInfo = function () {
            console.log("获取群信息");
            conn.queryRoomInfo({
                roomId:roomid,
                success: function (d) {
                    console.log(d);
                    if(d&& d.length>0){
                        for(var i=0;i<d.length;i++){
                            if(d[i].affiliation=="owner"){
                                //masterid= d[i].jid.match(/[pulu#pulu_0-9a-zA-Z]+/)[0].replace("pulu#pulu_","");
                                console.log("获取群主信息：",masterid);
                                console.log("当前ID:",id);
                                masterid==id?$("#delmemberbtn").css("display","block"):$("#delmemberbtn").css("display","none");
                                if(masterid==id){
                                    alert("我是群主");
                                    $("#delmemberbtn").css("display","block");
                                }
                                //==============================================================================================================
                                //删除成员返回按钮-------------------------
                                $("#delfriendgobackbtn").click(function(){
                                    $("#allchatmember").css("display","block");
                                    $("#delfriend").css("display","none");
                                });
//删除成员列表确定-------------------------
                                $("#delimgchoicedFDokbtn1").click(function(){
                                    var addmemberhash={};//哈希判断存在与唯一性
                                    var addlist=[];//删除成员数组
                                    if($("#delaa").css("display")=="block"){
                                        var allcheck=document.querySelectorAll("#delaa .addchatpeoplebtn,#dela1 .addchatpeoplebtn");
                                        var tim=0;
                                        for(var i= 0,len=allcheck.length;i<len;i++){
                                            tim++;
                                            if(allcheck[i].checked==true){
                                                if(addmemberhash[allcheck[i].dataset.fid]){
                                                    //是否存在 搜索列表
                                                }else{
                                                    addmemberhash[allcheck[i].dataset.fid]=allcheck[i].dataset.fid;
                                                    addlist.push(allcheck[i].dataset.fid);
                                                }
                                            }
                                            if(tim==len){
                                                //循环结束 删除成员 (只能一个一个删除 用循环)
                                                var deltim=0;
                                                for(var d=0;d<addlist.length;d++){
                                                    deltim++;
                                                    var option = {
                                                        affiliation: 'owner',      // 写死
                                                        roomId:roomid,              // 群组ID
                                                        success: function () {
                                                            console.log('删除群聊成员');
                                                        },
                                                        to:addlist[d]               // 需要删除的成员ID
                                                    };
                                                    conn.addToGroupBlackList(option);
                                                    //如果循环删除结束了
                                                    if(deltim==addlist.length){
                                                        //刷新成员页面
                                                        $(".imgbox").remove();
                                                        queryRoomMember();
                                                        var tim2=0;
                                                        for(var r= 0;r<len;r++){
                                                            allcheck[r].checked=false;
                                                            tim2++;
                                                            //清空操作记录 展开页面
                                                            if(tim2==len){
                                                                $("#allchatmember").css("display","block");
                                                                $("#imgchoicefriend").css("display","none");
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                });
//退出或者解散群---------------------------
                                console.log(id);
                                console.log(masterid);
                                if(id==masterid){  //是群主 就是删除
                                    $("#memdelbtn").click(function(){
                                        var option = {
                                            reason: '群主删除',
                                            roomId: roomid,
                                            success: function () {
                                                console.log('群主删除群聊');
                                            }
                                        };
                                        conn.destroyGroup(option);
                                    });
                                }else{
                                    $("#memdelbtn").click(function(){
                                        var option = {
                                            to: id,
                                            roomId: roomid,
                                            success: function () {
                                                console.log('You leave room succeed!');
                                            },
                                            error: function () {
                                                console.log('Leave room faild');
                                            }
                                        };
                                        conn.leaveGroupBySelf(option);
                                    });
                                }
//群主改群名-------------------------------
                                if(id==masterid){
                                    //展开改名页面
                                    $("#changenamebtn").click(function(){
                                        $("#allchatmember").css("display","none");
                                        $("#changename").css("display","block");
                                    });
                                    //改名页面返回
                                    $("#changenamegoback").click(function(){
                                        $("#allchatmember").css("display","block");
                                        $("#changename").css("display","none");
                                    });
                                    //有输入值按钮显示
                                    $('#changetitleinput').bind('input propertychange', function() {
                                        //判断输入值
                                        if(document.getElementById("#changetitleinput").value&&!document.getElementById("#changetitleinput").value.match(/^\s+$/g&&document.getElementById("#changetitleinput").value.length>0)){
                                            $("#changenameokbtn").attr("class","header_action_buttons canclick");
                                            //改名页面确定
                                            $("#changenameokbtn").click(function(){
                                                //修改群组信息
                                                var option = {
                                                    roomId:roomid,
                                                    subject:document.getElementById("#changetitleinput").value,    // 群组名称
                                                    description: 'Change group information test',  // 群组简介
                                                    success: function () {
                                                        console.log('Change Group Names Success!');
                                                    }
                                                };
                                                conn.changeGroupSubject(option);
                                            });
                                        }
                                        else{
                                            $("#changenameokbtn").attr("class","header_action_buttons cannotclick");
                                        }
                                    });
                                }
                                //==============================================================================================================
                            }
                        }
                    }
                },
                error: function () {
                    console.log('Error!');
                }
            });
        };
        queryGroupInfo();
        //获取环信成员信息----------------------------------
        queryRoomMember();

    },
    onTextMessage: function ( message ) {
        console.log("收到的文本消息");
        console.log(message);
        var arr1=message.data;
        console.log(arr1);
        var reg=/\[[^\s]+\]/g;
        if(arr1.match(reg)){
            var arr3=arr1.match(reg);
            console.log(arr3);
            var faceimg="";
            for(var rr= 0;rr<arr3.length;rr++){
                arr3[rr].index=rr;
                if(arr3[rr].match(reg)){
                    arr1=arr1.replace(arr3[rr],"<img src='faces/"+map[arr3[rr]]+"'>");
                };
                console.log(arr1);
            }
        }
        //获取接收消息的时间-----------
        var newtime=GetDateT();
        console.log(newtime);
        $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
        $(".img").height($(".img").width());
    },//收到文本消息
    onClosed: function ( message ) {console.log("1");},         //连接关闭回调
    onEmojiMessage: function ( message ) {console.log("2");},   //收到表情消息
    onPictureMessage: function ( message ) {
        console.log(message);
        //获取接收消息的时间-----------
        var newtime=GetDateT();
        console.log(newtime);
        $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p><img class='getimg' src='"+message.url+"'></div> </div>");
        $(".img").height($(".img").width());
    }, //收到图片消息
    onCmdMessage: function ( message ) {console.log("4");},     //收到命令消息
    onAudioMessage: function ( message ) {console.log("5");},   //收到音频消息
    onLocationMessage: function ( message ) {console.log("6");},//收到位置消息
    onFileMessage: function ( message ) {console.log("7");},    //收到文件消息
    onVideoMessage: function ( message ) {console.log("8");},   //收到视频消息
    onPresence: function ( message ) {console.log("9");console.log(message);},       //收到联系人订阅请求、处理群组、聊天室被踢解散等消息
    onRoster: function ( message ) {console.log("10");},         //处理好友申请
    onInviteMessage: function ( message ) {console.log("11");},  //处理群组邀请
    onOnline: function () {console.log("本机网络连接成功12");},                  //本机网络连接成功
    onOffline: function () {console.log("本机网络掉线13");}                 //本机网络掉线
    //onError: function ( message ) {console.log("失败回调14");console.log(message)}           //失败回调
});
//发送聊天信息
var sendword=document.getElementById("sendword");//按钮
//点击按钮或者键盘
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        if(document.getElementById("inputword").value){
            var id = conn.getUniqueId();//生成本地消息id
            var msg = new WebIM.message('txt', id);//创建文本消息
            //判断是否有表情
            var arr1=document.getElementById("inputword").value;
            var reg=/\[[^\s]+\]/g;
            //var arr3=arr1.split(" ");
            if(arr1.match(reg)){
                var arr3=arr1.match(reg);
                var faceimg="";
                for(var rr= 0;rr<arr3.length;rr++){
                    arr3[rr].index=rr;
                    if(arr3[rr].match(reg)){
                        arr1=arr1.replace(arr3[rr],"<img src='faces/"+map[arr3[rr]]+"'>");
                    };
                    console.log(arr1);
                }
            }
            //配置发送到群组
            var option = {
                msg:document.getElementById("inputword").value,             // 消息内容
                to: roomid,                     // 接收消息对象(群组id)
                roomType: false,
                chatType: 'chatRoom',
                success: function () {
                    console.log("发送信息成功");
                    console.log(id);
                    //console.log(serverId);
                    var newtime=GetDateT();
                    console.log(newtime);

                    $("#chatbody").append("<div class='mychatword'> <div class='img'>"+myimg+"</div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                    console.log($(".img").width());
                    $(".img").height($(".img").width());
                    $("#inputword").val("");
                },
                fail: function () {
                    console.log('failed');
                }
            };
            msg.set(option);
            msg.setGroup('groupchat');
            conn.send(msg.body);
        }
    }
};
sendword.onclick=function(){
    if(document.getElementById("inputword").value){
        var id = conn.getUniqueId();//生成本地消息id
        var msg = new WebIM.message('txt', id);//创建文本消息
        //判断是否有表情
        var arr1=document.getElementById("inputword").value;
        var reg=/\[[^\s]+\]/g;
        //var arr3=arr1.split(" ");
        if(arr1.match(reg)){
            var arr3=arr1.match(reg);
            var faceimg="";
            for(var rr= 0;rr<arr3.length;rr++){
                arr3[rr].index=rr;
                if(arr3[rr].match(reg)){
                    arr1=arr1.replace(arr3[rr],"<img src='faces/"+map[arr3[rr]]+"'>");
                };
                console.log(arr1);
            }
        }
        //配置发送到群组
        var option = {
            msg:document.getElementById("inputword").value,             // 消息内容
            to: roomid,                     // 接收消息对象(群组id)
            roomType: false,
            chatType: 'chatRoom',
            success: function () {
                console.log("发送信息成功");
                console.log(id);
                //console.log(serverId);
                $("#chatbody").append("<div class='mychatword'> <div class='img'>"+myimg+"</div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                console.log($(".img").width());
                $(".img").height($(".img").width());
                $("#inputword").val("");
            },
            fail: function () {
                console.log('failed');
            }
        };
        msg.set(option);
        msg.setGroup('groupchat');
        conn.send(msg.body);
        //=========================================
    }
};
//打开表情
$(".face").click(function(){
    if(document.getElementsByClassName("hide")[0].style.display==="block"){
        document.getElementsByClassName("hide")[0].style.display="none";
    }else if(document.getElementsByClassName("hide")[0].style.display==="none"){
        document.getElementsByClassName("hide")[0].style.display="block";
    }
});
//发送表情
function sendface(e){
    var face=e.dataset.key;
    //console.log(face);
    document.getElementById("inputword").value+=" "+face;
}
//上传图片
document.getElementById("addimgbtn").addEventListener( 'change',createimg,false );
function createimg(){
    var id = conn.getUniqueId();
    var msg = new WebIM.message('img', id);
    var input = document.getElementById('addimgbtn');//选择图片的input
    var file = WebIM.utils.getFileUrl(input);
    console.log(file);
    var allowType = {
        "jpg": true,
        "gif": true,
        "png": true,
        "bmp": true
    };
    if ( file.filetype.toLowerCase() in allowType ) {
        var imgurl="";
        msg.set({
            apiUrl: WebIM.config.apiURL,
            file: file,
            //to: otherid,
            to: roomid,                     // 接收消息对象(群组id)
            roomType: false,
            chatType: 'chatRoom',
            onFileUploadError: function ( error ) {
                console.log("发送图片失败");
            },//图片上传失败
            onFileUploadComplete: function ( data ) {
                console.log("发送图片部分");
                //图片地址：data.uri + '/' + data.entities[0].uuid;
                console.log(data.uri + '/' + data.entities[0].uuid);
                imgurl=data.uri + '/' + data.entities[0].uuid;
            },
            success: function ( id, serverMsgId ) {
                console.log("发送图片成功");
                console.log(id);
                console.log(serverMsgId);
                $("#chatbody").append("<div class='mychatword'> <div class='img'>"+myimg+"</div> <div class='word'><p class='arrow'></p><img class='getimg' src='"+imgurl+"'></div> </div>");
            },//图片消息发送成功
            flashUpload: WebIM.flashUpload
        });
        conn.send(msg.body);
    }
};
//加载好友列表----------------------------------------------
var friendarr;//保存所有好友数据
$.ajax({
    datatype:"json",
    type:"get",
    url:"http://42.62.66.198:9001/api/PuluGoodFriend/GetByUserId?userId="+id,
    success:function(d){
        console.log("好友列表");
        console.log(d);
        if(d.Data.length>0){
            //排序
            friendarr= d.Data;
            function compare(property){
                return function(a,b){
                    var value1 = pinyin.getCamelChars(a[property]);
                    var value2 = pinyin.getCamelChars(b[property]);
                    return value1.localeCompare(value2);
                }
            }
            friendarr.sort(compare("friendnick"));
            hasharr={};//判断字母是否存在
            //添加好友列表-------------------------
            for(var i=0;i<friendarr.length;i++){
                friendarr[this.index]=i;
                //如果昵称开头为汉字或英文字母
                var key=pinyin.getCamelChars(friendarr[i].friendnick[0]);
                if(key.match(/[a-z|A-Z]/)){
                    key=key.toLowerCase();
                    console.log(key);
                    if(!hasharr[pinyin.getCamelChars(friendarr[i].friendnick[0])]){
                        hasharr[pinyin.getCamelChars(friendarr[i].friendnick[0])]=i;
                    }
                    $("#maina").append("<div class='section_header section_header_gray "+key+"' data-num='"+i+"' style='display:none'> <span class='"+key+"'>"+key+"</span> </div><div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                    $("#aa").append("<div class='section_header section_header_gray "+key+"' data-num='"+i+"' style='display:none'> <span class='"+key+"'>"+key+"</span> </div><div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                }else{
                    $("#mainjinghao").css("display","block");
                    $("#mainjinghao").append("<div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                    $("#jinghao").css("display","block");
                    $("#jinghao").append("<div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                };
            }
            //删除好友列表-------------------------
            for(var i1=0;i1<friendarr.length;i1++){
                friendarr[this.index]=i1;
                //如果昵称开头为汉字或英文字母
                var key1=pinyin.getCamelChars(friendarr[i1].friendnick[0]);
                if(key1.match(/[a-z|A-Z]/)){
                    key1=key1.toLowerCase();
                    console.log(key1);
                    if(!hasharr[pinyin.getCamelChars(friendarr[i1].friendnick[0])]){
                        hasharr[pinyin.getCamelChars(friendarr[i1].friendnick[0])]=i1;
                    }
                    $("#delmaina").append("<div class='section_header section_header_gray "+key1+"' data-num='"+i1+"' style='display:none'> <span class='"+key1+"'>"+key1+"</span> </div><div class='friends' id='"+friendarr[i1].friendId+"'> <input type='checkbox' class='addchatpeoplebtn'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i1].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i1].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i1].friendnick+"</div> </div>");
                    $("#delaa").append("<div class='section_header section_header_gray "+key1+"' data-num='"+i1+"' style='display:none'> <span class='"+key1+"'>"+key1+"</span> </div><div class='friends' id='"+friendarr[i1].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i1].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i1].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i1].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i1].friendnick+"</div> </div>");
                }else{
                    $("#delmainjinghao").css("display","block");
                    $("#delmainjinghao").append("<div class='friends' id='"+friendarr[i1].friendId+"'> <input type='checkbox' class='addchatpeoplebtn'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i1].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i1].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i1].friendnick+"</div> </div>");
                    $("#deljinghao").css("display","block");
                    $("#deljinghao").append("<div class='friends' id='"+friendarr[i1].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i1].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i1].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i1].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i1].friendnick+"</div> </div>");
                };
            }
            for(var key in hasharr){
                key=key.toLowerCase();
                $("#delfriend .section_header.section_header_gray."+key).eq(0).css("display","block");
                $("#imgchoicefriend .section_header.section_header_gray."+key).eq(0).css("display","block");
            };
        }
    }
});
//右侧导航条点击跳转
//字母键
var englishbtn1=document.querySelectorAll(".rightnav1 div.zmbtn");//导航条跳转
var tonum1=0;
for(var en1=0;en1<englishbtn1.length;en1++){
    englishbtn1[en1].onclick=function(e){
        console.log("点击的是："+e.target.innerHTML);
        if($(".section_header.section_header_gray."+e.target.innerHTML).length>0){
            var engtitlenum=parseFloat(hasharr[e.target.innerHTML]);
            var divnum=parseFloat(document.querySelector(".section_header.section_header_gray."+e.target.innerHTML).dataset.num);
            var divheight=$(".friends").eq(0).height();
            var titleheight=$("div.section_header.section_header_gray").eq(0).height();
            heightdiv=titleheight*engtitlenum+divnum*divheight;
            $(window).scrollTop(heightdiv);
            console.log("页面跳转操作")
        }
    };
}
//搜索好友------------------------------------------------------
$("#csearchbtn1").click(function(){//搜索跳转
    var word=$("#csearchinput1").val();
    $("#aa").css("display","none");
    $("#a1 div").remove();
    if(friendarr&&friendarr.length>0&&$("#csearchinput1").val()){
        var word=$("#csearchinput1").val();
        for(var i=0;i<friendarr.length;i++){
            var friendnick="";
            friendnick=friendarr[i].friendnick;
            //如果好友列表中有搜索内容
            if(friendnick.match(word)){
                $("#a1").append("<div class='section_header section_header_gray' data-num='"+i+"' style='display:none'> </div><div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
            }
        }
        $("#a1").css("display","block");
    }
});
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        var word=$("#csearchinput1").val();
        $("#aa").css("display","none");
        $("#a1 div").remove();
        if(friendarr&&friendarr.length>0&&$("#csearchinput1").val()){
            var word=$("#csearchinput1").val();
            for(var i=0;i<friendarr.length;i++){
                var friendnick="";
                friendnick=friendarr[i].friendnick;
                //如果好友列表中有搜索内容
                if(friendnick.match(word)){
                    $("#a1").append("<div class='section_header section_header_gray' data-num='"+i+"' style='display:none'> </div><div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div></div>");
                }
            }
            $("#a1").css("display","block");
        }
    }
};
//监听好友搜索输入框---------------------------------------------
$("#csearchinput1").bind('input propertychange',function(){
    if(!$("#csearchinput1").val()){
        $("#aa").css("display","block");
        $("#a1").css("display","none");
    }
});
//展示群组成员-------------------------
$("#showmember").click(function(){
    //头部隐藏
    $("#mainpagetitle").css("display","none");
    //聊天部分隐藏
    $("#chatbody").css("display","none");
    //底部隐藏
    $(".bottomnav").css("display","none");
    //展示详情页
    $("#allchatmember").css("display","block");
});
//群组成员详情页返回按钮--------------
$("#allchatmembergoback").click(function(){
    //头部展示
    $("#mainpagetitle").css("display","block");
    //聊天部分展示
    $("#chatbody").css("display","block");
    //底部展示
    $(".bottomnav").css("display","block");
    //隐藏详情页
    $("#allchatmember").css("display","none");
});
//点击添加成员按钮-----------------------
$("#addmemberbtn").click(function(){
    $("#allchatmember").css("display","none");
    $("#imgchoicefriend").css("display","block");
});
//添加好友返回键------------------------
$("#friendgobackbtn").click(function(){
    $("#allchatmember").css("display","block");
    $("#imgchoicefriend").css("display","none");
});
//添加好友列表确定-------------------------- (ok)
$("#imgchoicedFDokbtn1").click(function(){
    var addmemberhash={};//哈希判断存在与唯一性
    var addlist=[];//添加成员数组
    if($("#aa").css("display")=="block"){
        var allcheck=document.querySelectorAll("#aa .addchatpeoplebtn,#a1 .addchatpeoplebtn");
        var tim=0;
        for(var i= 0,len=allcheck.length;i<len;i++){
            tim++;
            if(allcheck[i].checked==true){
                if(addmemberhash[allcheck[i].dataset.fid]){
                    //是否存在 搜索列表
                }else{
                    addmemberhash[allcheck[i].dataset.fid]=allcheck[i].dataset.fid;
                    addlist.push(allcheck[i].dataset.fid);
                }
            }
            if(tim==len){
                //循环结束 添加成员
                var option = {
                    list:addlist,
                    roomId:roomid
                };
                conn.addGroupMembers(option);
                //刷新成员页面
                $(".imgbox").remove();
                queryRoomMember();
                var tim2=0;
                for(var r= 0;r<len;r++){
                    allcheck[r].checked=false;
                    tim2++;
                    //清空操作记录 展开页面
                    if(tim2==len){
                        $("#allchatmember").css("display","block");
                        $("#imgchoicefriend").css("display","none");
                    }
                }
            }
        }
    }
});
//点击删除成员展开页面----------------------------
$("#delmemberbtn").click(function(){
    $("#allchatmember").css("display","none");
    $("#delfriend").css("display","block");
});
